package com.tvunetworks.center.device.config.memcache;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.spy.memcached.DefaultConnectionFactory;
import net.spy.memcached.MemcachedClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;

/**
* @ClassName: MemcacheClientServer  
* @author lebron  

*/
@Slf4j
@Configuration
public class MemcacheClientServer {

	private static final String LOCALHOST = "localhost";
	private static final int DEFAULT_PORT = 11211;
	@Value("${ccMemcache.TimeOut}")
	private String timeout;

	@Value("${ccMemcache.timeoutExceptionThreshold}")
	private String timeoutExceptionThreshold;

	@Autowired
	private Environment environment;

	public MemcacheClientServer() {}

//	@Bean(name = "tpdsMemcacheClient")
//	public MemcachedClient tpdsMemcacheClient() throws IOException {
//		String servers = this.environment.getProperty("tpdsMemcacheServer");
//		return configMemcachedClient(servers);
//	}

	/**
	 *
	 * @return  MemcachedClient
	 * @throws IOException 编译异常
	 */
	@Bean(name = "ccMemcacheClient")
	public MemcachedClient ccMemcacheClient() throws IOException {
		String servers = this.environment.getProperty("ccMemcacheServer");
		//ccMemcacheClient 的重连次数和等待超时时间改成可配置
		return configMemcachedClient(servers, Long.valueOf(timeout), Integer.parseInt(timeoutExceptionThreshold));
	}

	/**
	 *
	 * @return MemcachedClient
	 * @throws IOException 编译异常
	 */
	@Bean(name = "tpdsMemcacheClient")
	public MemcachedClient tpdsMemcacheClient() throws IOException {
		String servers = this.environment.getProperty("tpdsMemcacheServer");
		return configMemcachedClient(servers,2500L,3);
	}

	/**
	 *
	 * @return psMemcacheClient
	 * @throws IOException 编译异常
	 */
	@Bean(name = "psMemcacheClient")
	public MemcachedClient psMemcacheClient() throws IOException {
		String servers = this.environment.getProperty("psmemcacheIpList4TP");
		log.error("psMemcacheClient Init infor"+ JSONObject.toJSONString(servers));
		return configMemcachedClient(servers,2500L,3);
	}

	private MemcachedClient configMemcachedClient(String servers, Long timeout, int timeoutExceptionThreshold) throws IOException {
		ArrayList<InetSocketAddress> addresses = new ArrayList<InetSocketAddress>();
		if (StringUtils.isEmpty(servers)) {
			addresses.add(new InetSocketAddress(LOCALHOST, DEFAULT_PORT));
		} else {
			String[] serverArray = servers.split(",");
			int length = serverArray.length;
			for (int i = 0; i < length; ++i) {
				String server = serverArray[i];
				int colon = server.indexOf(":");
				if (colon == -1) {
					addresses.add(new InetSocketAddress(server, 11211));
				} else {
					int port = Integer.parseInt(server.substring(colon + 1));
					addresses.add(new InetSocketAddress(server.substring(0, colon), port));
				}
			}
		}



		return new MemcachedClient(new DefaultConnectionFactory() {
			@Override
			public long getOperationTimeout() {
				return timeout;
			}

			@Override
			public int getTimeoutExceptionThreshold() {
				return timeoutExceptionThreshold;
			}
		}, addresses);


	}
}
